package com.css.zfzx.fwsjcj.modules.structure.verify;

import cn.afterturn.easypoi.excel.entity.result.ExcelVerifyHandlerResult;
import cn.afterturn.easypoi.handler.inter.IExcelVerifyHandler;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author
 * @program
 * @description
 * @create 2022/2/14 14:42
 **/
public class HighStructureCheck implements IExcelVerifyHandler {
    /**
     * 错误信息
     */
    private StringBuilder msg;
    /**
     * 省市区地区map
     */
    private HashMap<String, List> map;


    public HighStructureCheck(HashMap map) {
        this.map = map;
    }

    public HighStructureCheck() {
    }

    @Override
    public ExcelVerifyHandlerResult verifyHandler(Object o) {
        // 默认为true
        ExcelVerifyHandlerResult result = new ExcelVerifyHandlerResult(true);
        msg = new StringBuilder();
        boolean hasMsg = isHasMsg(o);
        if (hasMsg) {
            result.setSuccess(false);
            result.setMsg(msg.toString());
        }
        return result;
    }

    /**
     * 是否有错误消息
     */
    private boolean isHasMsg(Object o) {
        if (isFieldHasMsg(o)) {
            msg.append("填写错误");
            return true;
        }
        return false;
    }

    /**
     * 校验字段是否市下拉列表中的字典值
     */
    private boolean isFieldHasMsg(Object o) {
        boolean flag = false;
        // 所属省份
        String province = getFieldValueByFieldName("belongtoProvince", o);
        if (province != null && !map.containsKey(province)) {
            msg.append("所属省份(填写全名称,检查省市县关系),");
            flag = true;
        } else {
            String city = getFieldValueByFieldName("belongtoCity", o);
            // 所属地级市
            if (!"北京市".equals(province) && !"天津市".equals(province) && !"上海市".equals(province) && !"重庆市".equals(province)) {
                List<String> cities = map.get(province);
                if (city != null && cities != null && !cities.contains(city)) {
                    msg.append("所属地级市(填写全名称,省市关系不匹配),");
                    flag = true;
                } else {
                    // 所属区县
                    String area = getFieldValueByFieldName("belongtoArea", o);
                    List<String> areas = map.get(city);
                    if (area != null && areas != null && !areas.contains(area)) {
                        msg.append("所属区县(填写全名称,市县关系不匹配),");
                        flag = true;
                    }
                }
            }
            // 所属地级市为直辖市
            else {
                if (city != null && !province.equals(city)) {
                    msg.append("所属地级市(填写全名称,直辖市应与省份同名)");
                    flag = true;
                } else {
                    // 所属区县
                    String area = getFieldValueByFieldName("area", o);
                    List<String> areas = map.get(city);
                    if (area != null && areas != null && !areas.contains(area)) {
                        msg.append("所属区县(填写全名称,市县关系不匹配)");
                        flag = true;
                    }
                }
            }
        }

        // 建造年代(下拉选项)
        String buildingAgeField = getFieldValueByFieldName("buildingAge", o);
        Map buildingAge=new HashMap<>();
        buildingAge.put("70s以前","");
        buildingAge.put("70s","");
        buildingAge.put("80s","");
        buildingAge.put("90s","");
        buildingAge.put("00s","");
        buildingAge.put("10s","");
        boolean buildingAgeBoo = buildingAge.containsKey(buildingAgeField);
        if (buildingAgeField != null && !buildingAgeBoo) {
            msg.append("建造年代(下拉选项),");
            flag = true;
        }

        // 结构形式(下拉选项)
        String structuralStyleField = getFieldValueByFieldName("structuralStyle", o);
        Map structuralStyle=new HashMap<>();
        structuralStyle.put("框架结构","");
        structuralStyle.put("框剪结构","");
        structuralStyle.put("剪力墙结构","");
        structuralStyle.put("框筒结构","");
        structuralStyle.put("筒体结构","");
        structuralStyle.put("钢结构","");
        structuralStyle.put("其他","");
        boolean structuralStyleBoo = structuralStyle.containsKey(structuralStyleField);
        if (structuralStyleField != null && !structuralStyleBoo) {
            msg.append("结构形式(下拉选项),");
            flag = true;
        }

        // 场地类型(下拉选项)
        String siteTypeField = getFieldValueByFieldName("siteType", o);
        Map siteType=new HashMap<>();
        siteType.put("I类","");
        siteType.put("II类","");
        siteType.put("III类","");
        siteType.put("IV类","");
        boolean siteTypeBoo = siteType.containsKey(siteTypeField);
        if (siteTypeField != null && !siteTypeBoo) {
            msg.append("场地类型(下拉选项),");
            flag = true;
        }

        // 基础类型(下拉选项)
        String baseTypeField = getFieldValueByFieldName("baseType", o);
        Map baseType=new HashMap<>();
        baseType.put("独立柱基础","");
        baseType.put("桩基础","");
        baseType.put("箱型基础","");
        baseType.put("条形基础","");
        baseType.put("筏板基础","");
        baseType.put("其他","");
        boolean baseTypeBoo = baseType.containsKey(baseTypeField);
        if (baseTypeField != null && !baseTypeBoo) {
            msg.append("基础类型(下拉选项),");
            flag = true;
        }

        // 设防标准(下拉选项)
        String fortifyStandardField = getFieldValueByFieldName("fortifyStandard", o);
        Map fortifyStandard=new HashMap<>();
        fortifyStandard.put("0（不设防）","");
        fortifyStandard.put("6（VI度设防）","");
        fortifyStandard.put("7（VII度设防）","");
        fortifyStandard.put("8（VIII度设防）","");
        fortifyStandard.put("9（IX度设防）","");
        boolean fortifyStandardBoo = fortifyStandard.containsKey(fortifyStandardField);
        if (fortifyStandardField != null && !fortifyStandardBoo) {
            msg.append("设防标准(下拉选项),");
            flag = true;
        }

        // 规则程度(下拉选项)
        String regularLevelField = getFieldValueByFieldName("regularLevel", o);
        Map regularLevel=new HashMap<>();
        regularLevel.put("1（平立面均规则）","");
        regularLevel.put("2（平面规则立面不规则）","");
        regularLevel.put("3（平面不规则立面规则）","");
        regularLevel.put("4（平面立面均不规则）","");
        boolean regularLevelBoo = regularLevel.containsKey(regularLevelField);
        if (regularLevelField != null && !regularLevelBoo) {
            msg.append("规则程度(下拉选项),");
            flag = true;
        }

        // 地下室结构形式(下拉选项)
        String basementStructuralStyleField = getFieldValueByFieldName("basementStructuralStyle", o);
        Map basementStructuralStyle=new HashMap<>();
        basementStructuralStyle.put("无地下室","");
        basementStructuralStyle.put("框架结构","");
        basementStructuralStyle.put("框剪结构","");
        basementStructuralStyle.put("剪力墙结构","");
        basementStructuralStyle.put("其他","");
        boolean basementStructuralStyleBoo = basementStructuralStyle.containsKey(basementStructuralStyleField);
        if (basementStructuralStyleField != null && !basementStructuralStyleBoo) {
            msg.append("地下室结构形式(下拉选项),");
            flag = true;
        }

        // 裙房结构形式(下拉选项)
        String skirtStructuralStyleField = getFieldValueByFieldName("skirtStructuralStyle", o);
        Map skirtStructuralStyle=new HashMap<>();
        skirtStructuralStyle.put("无裙房","");
        skirtStructuralStyle.put("砌体结构","");
        skirtStructuralStyle.put("框架结构","");
        skirtStructuralStyle.put("框剪结构","");
        skirtStructuralStyle.put("剪力墙结构","");
        skirtStructuralStyle.put("其他","");
        boolean skirtStructuralStyleBoo = skirtStructuralStyle.containsKey(skirtStructuralStyleField);
        if (skirtStructuralStyleField != null && !skirtStructuralStyleBoo) {
            msg.append("裙房结构形式(下拉选项),");
            flag = true;
        }

        // 柱混凝土强度等级(下拉选项)
        String colConcreteStrengthGradeField = getFieldValueByFieldName("colConcreteStrengthGrade", o);
        Map colConcreteStrengthGrade=new HashMap<>();
        colConcreteStrengthGrade.put("无","");
        colConcreteStrengthGrade.put("C15","");
        colConcreteStrengthGrade.put("C20","");
        colConcreteStrengthGrade.put("C25","");
        colConcreteStrengthGrade.put("C30","");
        colConcreteStrengthGrade.put("C35","");
        colConcreteStrengthGrade.put("C40","");
        colConcreteStrengthGrade.put("C45","");
        colConcreteStrengthGrade.put("C50","");
        colConcreteStrengthGrade.put("C55","");
        colConcreteStrengthGrade.put("C60","");
        colConcreteStrengthGrade.put("C65","");
        colConcreteStrengthGrade.put("C70","");
        colConcreteStrengthGrade.put("C75","");
        colConcreteStrengthGrade.put("C80","");
        colConcreteStrengthGrade.put("其他","");
        boolean colConcreteStrengthGradeBoo = colConcreteStrengthGrade.containsKey(colConcreteStrengthGradeField);
        if (colConcreteStrengthGradeField != null && !colConcreteStrengthGradeBoo) {
            msg.append("柱混凝土强度等级(下拉选项),");
            flag = true;
        }

        // 剪力墙混凝土强度等级(下拉选项)
        String shearwellConcreteStrengthGradeField = getFieldValueByFieldName("shearwellConcreteStrengthGrade", o);
        Map shearwellConcreteStrengthGrade=new HashMap<>();
        shearwellConcreteStrengthGrade.put("无","");
        shearwellConcreteStrengthGrade.put("C15","");
        shearwellConcreteStrengthGrade.put("C20","");
        shearwellConcreteStrengthGrade.put("C25","");
        shearwellConcreteStrengthGrade.put("C30","");
        shearwellConcreteStrengthGrade.put("C35","");
        shearwellConcreteStrengthGrade.put("C40","");
        shearwellConcreteStrengthGrade.put("C45","");
        shearwellConcreteStrengthGrade.put("C50","");
        shearwellConcreteStrengthGrade.put("C55","");
        shearwellConcreteStrengthGrade.put("C60","");
        shearwellConcreteStrengthGrade.put("C65","");
        shearwellConcreteStrengthGrade.put("C70","");
        shearwellConcreteStrengthGrade.put("C75","");
        shearwellConcreteStrengthGrade.put("C80","");
        shearwellConcreteStrengthGrade.put("其他","");
        boolean shearwellConcreteStrengthGradeBoo = shearwellConcreteStrengthGrade.containsKey(shearwellConcreteStrengthGradeField);
        if (shearwellConcreteStrengthGradeField != null && !shearwellConcreteStrengthGradeBoo) {
            msg.append("剪力墙混凝土强度等级(下拉选项),");
            flag = true;
        }

        // 填充墙体材质(下拉选项)
        String fillWallMaterialField = getFieldValueByFieldName("fillWallMaterial", o);
        Map fillWallMaterial=new HashMap<>();
        fillWallMaterial.put("普通砖","");
        fillWallMaterial.put("多孔砖","");
        fillWallMaterial.put("砌块","");
        fillWallMaterial.put("其他","");
        boolean fillWallMaterialBoo = fillWallMaterial.containsKey(fillWallMaterialField);
        if (fillWallMaterialField != null && !fillWallMaterialBoo) {
            msg.append("填充墙体材质(下拉选项),");
            flag = true;
        }

        // 填充墙砂浆类型(下拉选项)
        String fillWallMortarTypeField = getFieldValueByFieldName("fillWallMortarType", o);
        Map fillWallMortarType=new HashMap<>();
        fillWallMortarType.put("水泥砂浆","");
        fillWallMortarType.put("混合砂浆","");
        fillWallMortarType.put("泥浆","");
        boolean fillWallMortarTypeBoo = fillWallMortarType.containsKey(fillWallMortarTypeField);
        if (fillWallMortarTypeField != null && !fillWallMortarTypeBoo) {
            msg.append("填充墙砂浆类型(下拉选项),");
            flag = true;
        }

        // 填充墙砂浆强度(下拉选项)
        String fillWallMortarStrengthGradeField = getFieldValueByFieldName("fillWallMortarStrengthGrade", o);
        Map fillWallMortarStrengthGrade=new HashMap<>();
        fillWallMortarStrengthGrade.put("大于等于M2.5","");
        fillWallMortarStrengthGrade.put("小于M2.5","");
        boolean fillWallMortarStrengthGradeBoo = fillWallMortarStrengthGrade.containsKey(fillWallMortarStrengthGradeField);
        if (fillWallMortarStrengthGradeField != null && !fillWallMortarStrengthGradeBoo) {
            msg.append("填充墙砂浆强度(下拉选项),");
            flag = true;
        }

        // 屋盖类别(下拉选项)
        String roofTypeField = getFieldValueByFieldName("roofType", o);
        Map roofType=new HashMap<>();
        roofType.put("木屋盖","");
        roofType.put("现浇屋盖","");
        roofType.put("预制板","");
        roofType.put("彩钢板","");
        roofType.put("其他","");
        boolean roofTypeBoo = roofType.containsKey(roofTypeField);
        if (roofTypeField != null && !roofTypeBoo) {
            msg.append("屋盖类别(下拉选项),");
            flag = true;
        }

        // 楼盖类别(下拉选项)
        String floorTypeField = getFieldValueByFieldName("floorType", o);
        Map floorType=new HashMap<>();
        floorType.put("木楼板","");
        floorType.put("现浇楼板","");
        floorType.put("预制板","");
        floorType.put("其他","");
        boolean floorTypeBoo = floorType.containsKey(floorTypeField);
        if (floorTypeField != null && !floorTypeBoo) {
            msg.append("楼盖类别(下拉选项),");
            flag = true;
        }

        // 梁柱现状(下拉选项)
        String beamSituationField = getFieldValueByFieldName("beamSituation", o);
        Map beamSituation=new HashMap<>();
        beamSituation.put("无开裂","");
        beamSituation.put("有开裂","");
        beamSituation.put("多处开裂","");
        boolean beamSituationBoo = beamSituation.containsKey(beamSituationField);
        if (beamSituationField != null && !beamSituationBoo) {
            msg.append("梁柱现状(下拉选项),");
            flag = true;
        }

        // 填充墙现状(下拉选项)
        String fillWellSituationField = getFieldValueByFieldName("fillWellSituation", o);
        Map fillWellSituation=new HashMap<>();
        fillWellSituation.put("无明显裂缝","");
        fillWellSituation.put("有明显裂缝","");
        fillWellSituation.put("多处明显裂缝","");
        boolean fillWellSituationBoo = fillWellSituation.containsKey(fillWellSituationField);
        if (fillWellSituationField != null && !fillWellSituationBoo) {
            msg.append("填充墙现状(下拉选项),");
            flag = true;
        }

        // 基础现状(下拉选项)
        String baseSituationField = getFieldValueByFieldName("baseSituation", o);
        Map baseSituation=new HashMap<>();
        baseSituation.put("无不均匀沉陷","");
        baseSituation.put("有不均匀沉陷","");
        boolean baseSituationBoo = baseSituation.containsKey(baseSituationField);
        if (baseSituationField != null && !baseSituationBoo) {
            msg.append("基础现状(下拉选项),");
            flag = true;
        }


        //经度
        String longitudeField = getFieldValueByFieldName("longitude", o);
        if(longitudeField!=null && !"".equals(longitudeField)){
            double longitude=Double.valueOf(longitudeField);
            if(longitude>180.0 || longitude<-180.0){
                msg.append("经度,");
                flag = true;
            }
        }
        //纬度
        String latitudeField = getFieldValueByFieldName("latitude", o);
        if(!"".equals(latitudeField) && latitudeField!=null) {
            double latitude = Double.valueOf(latitudeField);
            if (latitude > 90.0 || latitude < -90.0) {
                msg.append("纬度,");
                flag = true;
            }
        }

        return flag;
    }





    /**
     * 根据属性名获取属性值
     *
     * @param fieldName 属性名
     * @param object    对象
     * @return
     */
    private String getFieldValueByFieldName(String fieldName, Object object) {
        try {
            String firstLetter = fieldName.substring(0, 1).toUpperCase();
            String getter = "get" + firstLetter + fieldName.substring(1);
            Method method = object.getClass().getMethod(getter, new Class[]{});
            Object value = method.invoke(object, new Object[]{});
            return value.toString();
        } catch (Exception e) {
            return null;
        }
    }
}
